#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :算数影视榜单.py
# @Time      :2024/11/27 
# @Author    :CL
# @email     :1037654919@qq.com
from scrapy_juliangsuanshu import *

# 算数影视榜单
def get_ent_index_weekly(start_date,end_date):
    print('begin function:get_ent_index_weekly')
    print('start_date=%s,end_date=%s'%(start_date,end_date))
    datestart = datetime.datetime.strptime(start_date, '%Y%m%d')
    dateend = datetime.datetime.strptime(end_date, '%Y%m%d')
    base_url ='https://trendinsight.oceanengine.com/api/open/brand/get_ent_index_weekly'
    while datestart<=dateend:
        date = datestart.strftime('%Y%m%d')
        print('当前进度：', date, end=' ')
        datestart += datetime.timedelta(days=7)
        datayy = pd.DataFrame()
        time.sleep(0.1)

        try:
            sign_url = 'https://trendinsight.oceanengine.com/api/open/brand/get_ent_index_weekly?start_date=%s&count=50&app_name=aweme&rank_key=score' % (
                date)
            headers2 = {}
            headers2['user-agent'] = str(UserAgent(verify_ssl=False).random)
            datastr = requests.get(sign_url, headers=headers2).json()['data']
            data = decrtptlx(datastr)
            requests.session().close()
            data = eval(data)

            categorys = ['movie','teleplay','show']
            for category in categorys:
                for value in data['%s'%category]['items']:
                    try:
                        datayy = datayy.append(
                            [{'category_name': data['%s' % category]['info']['category_name'],
                              'en_name': data['%s' % category]['info']['en_name'],
                              'item_id': value['item_id'],
                              'name': value['item_info'].get('name'),
                              'influence_score': value['item_stats']['influence_score'],
                              'discussion_score': value['item_stats']['discussion_score'],
                              'challenge_score': value['item_stats']['challenge_score'],
                              'search_score': value['item_stats']['search_score'],
                              'score': value['score'],
                              'rank': value['rank'],
                              'rank_change': value['rank_change'],
                              'rate_change': value['rate_change'],
                              'date': date,
                              'poster': value['item_info'].get('poster'),
                              }],
                            ignore_index=True)
                    except:print('something wrong with the %s,and we will pass this one'%(value['item_info']))

            print('%s success'%date)
        except:
            print('%s fialed'%date)
            pass
    return
